<?php
/**
 * User Social Network Connection
 * 
 * It will display the list of social network that the user is connected with
 * 
 * @author Xavier VILAIN
 * @package Hive\User
 * @license #license-hive-social#
 */

use Fox\Html\Block;
use Fox\Html\Screen;
use Hive\Ressource\Source\Parser\Linkedin;
use Fox\Html\Button;
use Fox\Html\Input;
use Fox\Generator;
use Fox\Html\Image;
use Fox\Html\Input\File;
use Fox\Html\Header\Content;
$xview = Xview::instance();
$id = $xview->application()->getId();
// Customizing
$customizing = Hive\User\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'users','act'=>'info','sac' => null));

// Capture LinkedIn Connection Code
$user = User::current();
$last_update = Msg(Hive::BUNDLE,Hive::LAST_UPDATE_ON,
					Fox\Utilities\DateTimes::toFullString($user->udate));
$last_update = '<br><span class="i sub-title-mini"> '.$last_update.'</span>';
$last_update = null;
// Page Title
$header = new Content('page-header',Msg(Hive::BUNDLE,Hive::MENU_USER_PERSONAL_INFORMATION).$last_update);
$html = null;

$data = User::current()->getData();
$data['profile_picture'] = User::current()->profilePictureUrl();
$screen = new Screen('sUserInfo','isave');
$screen->action(Generator::url('users','info',null,array('xvtrs'=>'none')));
//$screen->ajaxUpload(Generator::redirect(Generator::url('users','main')));
$screen->ajaxUpload(Generator::redirect(Generator::url('users','main')),'contentArea');
$screen->buttonBarClass('text-center');
$screen->block()->add(new Input('email',false))
		->size(Input::SIZE_XLARGE)
		->input(false);
$screen->block()->add(new Input('fname',true))
		;
$screen->block()->add(new Input('lname',true))
		;
// Image Block
$blImage = new Block('imgBl');
$blImage->message('<p class="prettyprint">'.Msg(Hive::BUNDLE,Hive::USER_PROFILE_PICTURE_HELP).'<p>');
$blImage->add(new Image('profile_picture',_APPLICATION_DEFAULT_PROFILE_PICTURE_))
				->add(new File('profile_picture_source'))
				->filter(File::$_FILTER_IMAGES_ONLY)
				->hide();
$screen->add($blImage);
$screen->initialize($data);
if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors()) {
		try {
			$user = User::current();
			// Manage File Upload
			if(!empty($fields['name'])) {
				
				$fileName = \Xview::moveFileToServer('profile_picture_source',
													_APPLICATION_IMAGES_PROFILE_PATH_,
													'user_picture_'.$user->getId());
				// Create Document Object
				$document = new \Fox\Document();
				$data['filename'] = $fileName['filename'];
				$data['destination'] = _APPLICATION_IMAGES_PROFILE_PATH_;
				$data['language'] = $user->language();
				$data['doctype'] = User::PROFILE_PICTURE;
				$data['physicalname'] = $fileName['physicalname'];
				$document->setFields($data);
				$document->create();
				$realImage = new \Fox\Core\Image($data['destination'].$fileName['physicalname']);
				$fileNameMini = 'thumb_'.$fileName['physicalname'];
				$realImage->resize(40,40);
				$realImage->export($data['destination'].$fileNameMini);
				// Add Document into the passed Object
				$user->setPrefs(User::PROFILE_PICTURE_PREFERENCE,$document->getId());
			}
			$user->setFields($fields);
			$user->update();
			Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::UPDATE_DONE));
		} catch(\Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		}
	}
	if(!$screen->errors()) {
		echo $xview->messages(true,true);
		exit;
	}
}
$html = $screen->display();
?>
<div>
	<script data-type="direct" language="javascript" type="text/javascript">
	<!--
	$(function(){
		$("#profile_picture").click(function(e){
			e.preventDefault();
			$("#profile_picture_source").click();
			return false;
		});
	});
	//-->
	</script>
	<div class="page-header clearfix "><?php echo $header->render();?></div>
	<div class="contentArea" id="contentArea">
		<div class="mbs ">
			<?php echo $html;?>
		</div>
	</div>
</div>